package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.AdviceDao;
import com.itheima.pojo.Advice;
import com.itheima.pojo.Report;
import com.itheima.service.AdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = AdviceService.class)
@Transactional
public class AdviceServiceImpl implements AdviceService {

    @Autowired
    private AdviceDao adviceDao;

    @Override
    public void add(Advice advice) {
        //获取到当前健康师的名字
        String username = advice.getUsername();
        Integer orderId = advice.getOrderId();
        String healthadvices = advice.getHealthadvices();

        Report report = new Report(healthadvices,username,orderId);
        adviceDao.add(report);

        Integer[] foodIds = advice.getFoodIds();
        Integer[] medicineIds = advice.getMedicineIds();
        Integer[] travelIds = advice.getTravelIds();


        Integer reportId = report.getId();
        //添加report与food关联表
        if (foodIds != null && foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("food_id",foodId);
                adviceDao.addReportAndFood(map);
            }
        }

        //添加report与medicine关联表信息
        if (medicineIds != null && medicineIds.length>0){
            for (Integer medicineId : medicineIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("medicine_id",medicineId);
                adviceDao.addReportAndMedicine(map);
            }
        }


        //添加report与travel关联表信息
        if (travelIds != null && travelIds.length>0){
            for (Integer travelId : travelIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("travel_id",travelId);
                adviceDao.addReportAndTravel(map);
            }
        }
    }

    @Override
    public int findByOrderId(int orderId) {
        return adviceDao.findByOrderId(orderId);
    }


    //数据回显
    public Advice findAllReportByOrderId(int id) {
        Report report = adviceDao.findById(id);

        //根据reportId获取到对应的药物，运动，膳食数据
        List<Integer> medicineList = adviceDao.findMedicinesByReportId(report.getId());
        Integer[] medicines = new Integer[medicineList.size()];
        for (int i = 0; i < medicineList.size(); i++) {
            medicines[i] = medicineList.get(i);
        }

        List<Integer> travelList = adviceDao.findTravelByReportId(report.getId());
        Integer[] travels = new Integer[travelList.size()];
        for (int i = 0; i < travelList.size(); i++) {
            travels[i] = travelList.get(i);
        }

        List<Integer> foodList = adviceDao.findFoodByReportId(report.getId());
        Integer[] foods = new Integer[foodList.size()];
        for (int i = 0; i < foodList.size(); i++) {
            foods[i] = foodList.get(i);
        }

        Advice advice = new Advice();
        advice.setFoodIds(foods);
        advice.setMedicineIds(medicines);
        advice.setTravelIds(travels);
        advice.setHealthadvices(report.getHealthadvices());
        advice.setOrderId(report.getOrderId());
        return advice;
    }

    //修改
    @Override
    public void edit(Advice advice) {
        Integer orderId = advice.getOrderId();
        String healthadvices = advice.getHealthadvices();
        String username = advice.getUsername();
        Integer[] foodIds = advice.getFoodIds();
        Integer[] medicineIds = advice.getMedicineIds();
        Integer[] travelIds = advice.getTravelIds();

        //修改order表
        Report report = new Report(healthadvices,username,orderId);
        adviceDao.edit(report);

        //修改之后的表id
        Report newReport = adviceDao.findById(orderId);
        Integer reportId = newReport.getId();

        //删除关联表数据
        adviceDao.deleteFoodByReportId(reportId);
        adviceDao.deleteMedicineByReportId(reportId);
        adviceDao.deleteTravelByReportId(reportId);

        //添加新的关联
        //添加report与food关联表
        if (foodIds != null && foodIds.length>0){
            for (Integer foodId : foodIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("food_id",foodId);
                adviceDao.addReportAndFood(map);
            }
        }

        //添加report与medicine关联表信息
        if (medicineIds != null && medicineIds.length>0){
            for (Integer medicineId : medicineIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("medicine_id",medicineId);
                adviceDao.addReportAndMedicine(map);
            }
        }


        //添加report与travel关联表信息
        if (travelIds != null && travelIds.length>0){
            for (Integer travelId : travelIds) {
                Map map = new HashMap();
                map.put("report_id",reportId);
                map.put("travel_id",travelId);
                adviceDao.addReportAndTravel(map);
            }
        }


    }
}
